﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using Models;
using Common;

namespace DAL
{
    /// <summary>
    /// 附加装饰数据处理类
    /// </summary>
    public  class OtherDecDAL
    {
        //获取主其他装饰主列表
        public DataTable GetOtherMainList(string decname,string mxbhid) {
            string sql = string.Empty;
            if (!string.IsNullOrWhiteSpace(decname))
            {
                sql += " and decname like '%" + decname + "%' ";
            }
            if (!string.IsNullOrWhiteSpace(mxbhid))
            {
                sql += " and mxbhid like '%" + mxbhid + "%' ";
            }
            return SqlHelper.ExecuteDataTable("select * from otherdecoration where isdel=0  " + sql + "  and decparentid=0");
        }
        //根据父类id获取子表内容
        public DataTable GetOtherDetailList(string pid) {
            return SqlHelper.ExecuteDataTable("select oid as cid,mxbhid as cmxbhid,decname as cdecname,limitmx as climitmx from otherdecoration where isdel=0 and decparentid=@pid", new MySqlParameter("@pid", pid));
        }
        //转换为主表类
        public OtherMainDec ToOtherMainDec(DataRow dr) {
            OtherMainDec om = new OtherMainDec();
            om.Oid = Convert.ToInt32(dr["oid"]);
            om.DecName = dr["decname"].ToString();
            om.Mxbhid = dr["mxbhid"].ToString();
            return om;
        }
        //子表
        public OtherDetailDec ToOtherDetailDec(DataRow dr) {
            OtherDetailDec od = new OtherDetailDec();
            od.Cid = Convert.ToInt32(dr["oid"]);
            od.CLimitMx = dr["limitmx"].ToString();
            od.CMxbhId = dr["MxbhId"].ToString();
            od.CDecName = dr["DecName"].ToString();
            od.CDecParentName = dr["DecParentName"].ToString();
            od.CDecParentId = dr["decparentid"].ToString();
            return od;
        }
        //保存主表
        public int SaveMainOtherDec(OtherMainDec od) {
            return SqlHelper.ExecuteNonQuery(@"insert into otherdecoration (decname,mxbhid,decparentid,adduser,edituser,addtime,edittime) values(@decname,@mxbhid,'0',@adduser,@edituser,@addtime,@edittime )",
                new MySqlParameter("@decname", od.DecName), new MySqlParameter("@mxbhid",od.Mxbhid),new MySqlParameter("@adduser",RemeberName.Username),new MySqlParameter("@edituser",RemeberName.Username),
                new MySqlParameter("@addtime", DateTime.Now.ToString()), new MySqlParameter("@edittime", DateTime.Now.ToString()));
        }
        //保存子表
        public int SaveDetailOtherDec(OtherDetailDec od) {
            return SqlHelper.ExecuteNonQuery(@"insert into otherdecoration (decname,mxbhid,decparentid,decparentname,limitmx,adduser,edituser,addtime,edittime) values(@decname,@mxbhid,@decparentid,@decparentname,@limitmx,@adduser,@edituser,@addtime,@edittime)",
                new MySqlParameter("@decname", od.CDecName), new MySqlParameter("@mxbhid", od.CMxbhId), new MySqlParameter("@decparentid", od.CDecParentId), new MySqlParameter("@decparentname", od.CDecParentName), new MySqlParameter("@limitmx", od.CLimitMx),
                new MySqlParameter("@adduser",RemeberName.Username),new MySqlParameter("@edituser",RemeberName.Username),
                new MySqlParameter("@addtime", DateTime.Now.ToString()), new MySqlParameter("@edittime", DateTime.Now.ToString()));
        }
        //更新主表
        public int UpdateMainOtherDec(OtherMainDec od)
        {
            return SqlHelper.ExecuteNonQuery(@"update otherdecoration set decname =@decname,mxbhid=@mxbhid,edituser=@edituser,edittime=@edittime where oid =@oid", new MySqlParameter("@decname", od.DecName), new MySqlParameter("@mxbhid",od.Mxbhid),
                new MySqlParameter("@edituser", RemeberName.Username), new MySqlParameter("@edittime", DateTime.Now.ToString()),new MySqlParameter("@oid",od.Oid));
        }

        //更新主表
        public int UpdateMainAndDetailOtherDec(OtherMainDec od)
        {
            int i = 0;
            i+= SqlHelper.ExecuteNonQuery(@"update otherdecoration set decname =@decname,mxbhid=@mxbhid,edituser=@edituser,edittime=@edittime where oid =@oid", new MySqlParameter("@decname", od.DecName), new MySqlParameter("@mxbhid", od.Mxbhid),
                new MySqlParameter("@edituser", RemeberName.Username), new MySqlParameter("@edittime", DateTime.Now.ToString()), new MySqlParameter("@oid", od.Oid));
            i += SqlHelper.ExecuteNonQuery(@"update otherdecoration set decparentName =@decparentName,mxbhid=@mxbhid,edituser=@edituser,edittime=@edittime where decparentid =@oid", new MySqlParameter("@decparentName", od.DecName), new MySqlParameter("@mxbhid", od.Mxbhid),
                new MySqlParameter("@edituser", RemeberName.Username), new MySqlParameter("@edittime", DateTime.Now.ToString()), new MySqlParameter("@oid", od.Oid));
             return i;
        }

        //更新子表
        public int UpdateDetailOtherDec(OtherDetailDec od)
        {
            return SqlHelper.ExecuteNonQuery(@"update otherdecoration set decname =@cdecname,mxbhid=@cmxbhid,decparentid=@cdecparentid,limitmx=@climitmx,edituser=@edituser,edittime=@edittime where oid =@oid", new MySqlParameter("@cdecname", od.CDecName),
                new MySqlParameter("@cmxbhid", od.CMxbhId), new MySqlParameter("@cdecparentid", od.CDecParentId), new MySqlParameter("@climitmx", od.CLimitMx), new MySqlParameter("@edituser", RemeberName.Username),
                new MySqlParameter("@edittime", DateTime.Now.ToString()), new MySqlParameter("@oid", od.Cid));
        }
        //编辑时获取主表信息
        public OtherMainDec GetOtherMainDecById(string id) {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from otherdecoration where isdel=0 and oid =@oid",new MySqlParameter("@oid",id));
            if (dt == null || dt.Rows.Count <= 0) {
                return null;
            }
            else if (dt.Rows.Count > 1)
            {
                throw new Exception("存在重复用户");
            }
            else {
                return ToOtherMainDec(dt.Rows[0]);
            }
        }
        //编辑时获取字表信息
        public OtherDetailDec GetOtherDetailDecById(string id) {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from otherdecoration where isdel=0 and oid =@oid", new MySqlParameter("@oid", id));
            if (dt == null || dt.Rows.Count <= 0)
            {
                return null;
            }
            else if (dt.Rows.Count > 1)
            {
                throw new Exception("存在重复用户");
            }
            else
            {
                return ToOtherDetailDec(dt.Rows[0]);
            }
        }
        //删除主表信息
        public bool DelOtherMainDecById(string id)
        {
            int result = SqlHelper.ExecuteNonQuery("update otherdecoration set isdel=1 ,editTime=@editTime,editUser=@editUser where oid =@oid",
                new MySqlParameter("@editTime", DateTime.Now.ToString()), new MySqlParameter("@editUser", RemeberName.Username), new MySqlParameter("@oid", id));
            return result > 0 ? true : false;

        }
        //主表信息之前先查看是否有子项信息
        public DataTable CheckOtherDetailDecByParentId(string id) {
            return SqlHelper.ExecuteDataTable("select * from otherdecoration where isdel=0 and decparentid=@decparentid",new MySqlParameter("@decparentid",id));
        }
        //删除子表信息
        public bool DelOtherDetailDecById(string id)
        {
            int result = SqlHelper.ExecuteNonQuery("update otherdecoration set isdel=1 ,editTime=@editTime,editUser=@editUser where oid =@oid",
                new MySqlParameter("@editTime", DateTime.Now.ToString()), new MySqlParameter("@editUser", RemeberName.Username), new MySqlParameter("@oid", id));
            return result > 0 ? true : false;

        }
        //获取主项下拉列表
        public DataTable GetMainList() {
            return SqlHelper.ExecuteDataTable("select * from otherdecoration where isdel=0 and decparentid=0");
        }


    }
}
